package com.woniu.humanresource.controller;


import com.woniu.humanresource.entity.*;
import com.woniu.humanresource.exception.GeneralException;
import com.woniu.humanresource.service.EmpInfoService;
import com.woniu.humanresource.service.EmployeesService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
public class EmployeesController {
    @Value("${upload.addResourceLocation}")
    private String addResourceLocation;
    @Resource
    EmployeesService employeesService;
    @Resource
    EmpInfoService empInfoService;

    //分页员工信息
    @PostMapping("/employees/{pageIndex}/{pageSize}")
    public ResponseText<PageBean<Employees>> getEmployeess(@PathVariable("pageIndex") int page,
                                                         @PathVariable("pageSize") int pageSize,
                                                         @RequestBody Employees employees
                                                           ){
        ResponseText<PageBean<Employees>> responseText=null;

        try {
            PageBean<Employees> pageBean = employeesService.selectEmpInfoCondition(employees, page, pageSize);
            responseText=new ResponseText<PageBean<Employees>>(pageBean,200,"success");
        } catch (Exception e) {
            e.printStackTrace();
            throw new GeneralException();
        }
        return responseText;
    }
    //添加员工信息
    @PostMapping("/employees")
    public ResponseText addEmployees(@RequestBody Employees employees){
        ResponseText responseText=null;
        try {
            employeesService.insertEmployees(employees);
            responseText=new ResponseText(null,200,"success");
        } catch (Exception e) {
            e.printStackTrace();
            throw new GeneralException();
        }
        return responseText;
    }
    //修改员工信息
    @PutMapping("/employees")
    public ResponseText updateEmployees(@RequestBody Employees employees){
        ResponseText responseText=null;
        try {

            employeesService.updateEmployees(employees);
            responseText=new ResponseText<PageBean<Employees>>(null,200,"success");
        } catch (Exception e) {
            e.printStackTrace();
            throw new GeneralException();
        }
        return responseText;
    }
    //删除员工信息
    @DeleteMapping("/employees/{id}")
    public ResponseText deleteEmployees(@PathVariable("id") int id){
        ResponseText responseText=null;
        try {
//            查询出员工
            Employees employees = new Employees();
            employees.setId(id);
            PageBean<Employees> pageBean = employeesService.selectEmpInfoCondition(employees, 1, 10);
            Employees employees1 = pageBean.getBeanList().get(0);
//            获取eid,传递给详情表，删除详情表内容
            empInfoService.deleteEmpInfo(employees1.getEid());
            employeesService.deleteEmployees(id);
            responseText=new ResponseText(null,200,"success");
        } catch (Exception e) {
            e.printStackTrace();
            throw new GeneralException();
        }
        return responseText;
    }

    //    上传头像
    @RequestMapping("/uploadEmployees")
    public ResponseText upload(MultipartFile head) throws IOException {
        try {
            DateFormat dateFormat=new SimpleDateFormat("yyyy.MM.dd");
            String dateStr = dateFormat.format(new Date());
            File file1 = new File(addResourceLocation+"head."+dateStr);
            if(!file1.exists()){
                file1.mkdirs();
            }
            String path=addResourceLocation+"head."+dateStr;
            File file = new File(path, UUID.randomUUID()+head.getOriginalFilename());
            head.transferTo(file);
            return new ResponseText(file.getPath(),200,"success");
        } catch (Exception e) {
            e.printStackTrace();
            throw new GeneralException();
        }
    }

    //导入员工信息
    @PostMapping("/importEmp")
    public ResponseText importEmployees(MultipartFile empExcel){
        ResponseText responseText=null;
        try {
          employeesService.importEmployees(empExcel);
            responseText=new ResponseText(null,200,"success");
        } catch (Exception e) {
            e.printStackTrace();
            throw new GeneralException();
        }
        return responseText;
    }
//    导出员工信息
    @GetMapping("/exportEmp")
    public ResponseText exportEmployeeExcel(HttpServletResponse response) {
        ResponseText responseText=null;
        try {
            employeesService.exportEmployeeExcel(response);
            responseText=new ResponseText(null,200,"success");
        }catch (Exception e){
            e.printStackTrace();
            throw new GeneralException();
        }
        return responseText;
    }
    //    查询活动
    @PostMapping("/empActivity/{id}")
    public ResponseText<List<Activity>> getActivity(@PathVariable("id") int id){
        ResponseText<List<Activity>> responseText=null;
        try {
            List<Activity> empActivity = employeesService.getEmpActivity(id);
            responseText=new ResponseText<List<Activity>>(empActivity,200,"success");
        } catch (Exception e) {
            e.printStackTrace();
            throw new GeneralException();
        }
        return responseText;
    }
}
